package com.lelouch.product.controller;

import com.google.common.collect.Lists;
import com.lelouch.product.bean.ProductCategory;
import com.lelouch.product.bean.ProductInfo;
import com.lelouch.product.common.DecreaseStockInput;
import com.lelouch.product.common.ProductInfoOutput;
import com.lelouch.product.common.ProductStatus;
import com.lelouch.product.service.ProductCatogoryService;
import com.lelouch.product.service.ProductService;
import com.lelouch.product.vo.ProductInfoVo;
import com.lelouch.product.vo.ProductVo;
import com.lelouch.product.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品控制器
 *
 * @author lcy
 * @create 2018-06-28 09:22
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCatogoryService productCatogoryService;

    /**
     * 查询所有在架的商品
     * @return
     */
    @RequestMapping("/list")
    public ResultVo list() {

        List<ProductInfo> allUpProductList = productService.findByProductStatus(ProductStatus.ON_LINE.getCode());

        List<Integer> allUpCategoryIdList = allUpProductList.stream().map(ProductInfo::getCategoryType).collect(Collectors.toList());

        List<ProductCategory> productCategories = productCatogoryService.findByCategoryTypeIn(allUpCategoryIdList);

        List<ProductVo> productVos = Lists.newArrayList();
        for(ProductCategory productCategory:productCategories){
            List<ProductInfoVo> productInfoVos = Lists.newArrayList();
            ProductVo productVo = ProductVo.builder().categoryName(productCategory.getCategoryName())
                    .categoryType(productCategory.getCategoryType()).build();
            for(ProductInfo productInfo:allUpProductList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVo productInfoVo = ProductInfoVo.builder()
                            .productId(productInfo.getProductId())
                            .productName(productInfo.getProductName())
                            .productDescription(productInfo.getProductDescription())
                            .productPrice(productInfo.getProductPrice())
                            .productIcon(productInfo.getProductIcon()).build();
                    productInfoVos.add(productInfoVo);
                }
            }
            productVo.setCategoryFoods(productInfoVos);
            productVos.add(productVo);

        }
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(0);
        resultVo.setMsg("成功");
        resultVo.setData(productVos);

        return resultVo;
    }

    /**
     * 获取商品列表(给订单服务用的)
     * @param productIdList
     * @return
     */
    @PostMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {
        return productService.findByProductIdIn(productIdList);
    }

    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList) {
        productService.decreaseStock(decreaseStockInputList);
    }


    @RequestMapping("/productMsg")
    public String productMsg(){
        return "this is product msg";
    }

}
